import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './user.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User)
    private usersRepository: Repository<User>,
  ) {}

  findAll(): Promise<User[]> {
    return this.usersRepository.find();
  }

  findAllAndCount(): Promise<[User[], number]> {
    return this.usersRepository.findAndCount();
  }

  findOne(id): Promise<User> {
    return this.usersRepository.findOne({ where: { id } });
  }

  createUser(body): Promise<User> {
    return this.usersRepository.save(body);
  }

  async delete(id): Promise<boolean> {
    const res = await this.usersRepository.delete(id);
    return res.affected > 0;
  }

  async queryUser(id): Promise<User> {
    return this.usersRepository.query(`SELECT * FROM USER WHERE id>?`, [id]);
  }
}
